package com.navi168.fwq21.util;

import com.amap.mapapi.location.LocationManagerProxy;
import com.amap.mapapi.location.LocationProviderProxy;
import com.navi168.fwq21.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class IndexPage extends Activity {
	private String SPF = "NAVI168";
	private LocationManagerProxy mLocationManager;
	private String net_info = "网络连接不正常...\n";
	private String gps_info = "GPS不正常...";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.index);
		SharedPreferences.Editor editor = getSharedPreferences(SPF,
				MODE_PRIVATE).edit();
		editor.putString("version", "0.8");
		editor.commit();
		Context context = this.getApplicationContext();
		mLocationManager = LocationManagerProxy.getInstance(this);
		// 界面动画
		ImageView imageView = (ImageView) findViewById(R.id.image_kj);
		imageView.setImageResource(R.drawable.logo);

		if (checkNet(context)) {
			net_info = "网络连接正常...";

		}

		if (checkGps()) {
			gps_info = "GPS连接正常...";
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Toast.makeText(getApplicationContext(), net_info, Toast.LENGTH_SHORT)
				.show();
		
		Toast.makeText(getApplicationContext(), gps_info, Toast.LENGTH_SHORT)
				.show();
		AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);
		aa.setDuration(2000);
		imageView.startAnimation(aa);
		aa.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				Intent intent = new Intent(IndexPage.this, MainPage.class);
				startActivity(intent);
				finish();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationStart(Animation animation) {

			}
		});

	}

	public static boolean checkNet(Context context) {
		// 获得手机所有连接管理对象（包括对wi-fi等连接的管理）
		try {
			ConnectivityManager connectivity = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				// 获得网络连接管理的对象
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					// 判断当前网络是否已连接
					if (info.getState() == NetworkInfo.State.CONNECTED)
						;

					return true;
				}
			}
		} catch (Exception e) {
		}
		return false;
	}

	public boolean checkGps() {
		boolean result = false;
		// 获取当前可用的Provider,其中MapABCNetwork为MapABC网络定位(基站和WiFi)
		for (final String provider : mLocationManager.getProviders(true)) {
			if (LocationManagerProxy.GPS_PROVIDER.equals(provider)
					|| LocationProviderProxy.MapABCNetwork.equals(provider)) {
				Log.i("mylog", "provider:" + provider);
				result = true;

			}
		}
		return result;
	}

	private boolean isNewVersion() {
		return false;

	}
}
